package com.huilang.stream.demo1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class StreamDemo1 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "a", "bc", "c", "bdb", "e", "f", "g", "h","a","b","c","d","e","f","g","h");
        //Stream流只能用一次，建议使用链式编程
        //filter()方法过滤
        list.stream().filter(s -> s.startsWith("b")).forEach(System.out::println);

        /**
         * 匿名内部类
         list.stream().filter(new Predicate<String>() {
        @Override
        public boolean test(String s) {
        return s.startsWith("b");
        }
        }).forEach(System.out::println);
         */
        System.out.println("=========");
        list.stream().limit(20).forEach(System.out::println);

        System.out.println("=========");
        //跳过
        list.stream().skip(6).forEach(System.out::println);
        System.out.println("=========");
        list.stream().skip(1).limit(3).forEach(System.out::println);

        System.out.println("=========");
        //去重
        list.stream().distinct().forEach(System.out::println);

        System.out.println("=========");

        ArrayList<String> list1 = new ArrayList<>();
        Collections.addAll(list1, "a-1", "bc-2", "c-3", "bdb-4", "e-5", "f-6", "g-7", "h-8","1-9");
        Stream.concat(list.stream(),list1.stream()).distinct().forEach(System.out::println);

        System.out.println("=========");

        list1.stream().forEach(s -> {
            String[] split = s.split("-");
            System.out.println(Integer.parseInt(split[1]));

        });
        System.out.println("=========");

        list1.stream().map(s ->Integer.parseInt(s.split("-")[1]) ).forEach(System.out::println);



    }
}
